package com.quanyan;

import com.alibaba.dubbo.common.json.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.search.entity.request.ReqClubMemberInfo;
import com.quanyan.search.entity.request.ReqClubSearchInfo;
import com.quanyan.search.entity.request.ReqPlaceSearchInfo;
import com.quanyan.search.entity.vo.ClubMemberIndexInfo;
import com.quanyan.search.entity.vo.PlaceIndexInfo;
import com.quanyan.search.service.ClubMemberSearchService;
import com.quanyan.search.service.ClubSearchService;
import com.quanyan.search.service.PlaceSearchService;
import com.quanyan.search.service.impl.ClubMemberSearchServiceImpl;
import com.quanyan.search.service.impl.ClubSearchServiceImpl;
import com.quanyan.search.service.impl.PlaceSearchServiceImpl;
import com.quanyan.search.utils.SolrServerSingle;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.MapSolrParams;
import org.junit.Test;

import java.io.IOException;
import java.util.*;

/**
 * SolrTest
 *
 * @author Tu Chun
 * @date 2016/6/12
 */

public class SolrTest2 {
    //private final static String url = "http://192.168.30.181:8080/solr";
    //public final static String url = "192.168.30.181:2181,192.168.30.181:2182,192.168.30.181:2183";
    public final static String url = "192.168.30.75:2181,192.168.30.75:2182,192.168.30.75:2183";

    private PlaceSearchService placeSearchService=new PlaceSearchServiceImpl(url);
    private ClubMemberSearchService clubMemberSearchService=new ClubMemberSearchServiceImpl(url);
    private ClubSearchService clubSearchService=new ClubSearchServiceImpl(url);


    @Test
    public void testDeleteByIdService() {

        try {
//            placeSearchService.deleteDocById(36);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testQueryByIdService() {

        try {
//            APIResponse<PlaceIndexInfo> indexs= placeSearchService.searchPlaceInfo(45);
//            System.out.println(JSON.json(indexs.getData()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testDeleteById() {

        try {
            //创建连接
            CloudSolrServer solrServer = SolrServerSingle.getInstance(url);
            // 设置默认的collection
            solrServer.setDefaultCollection("collection2");

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("place_id:").append(45);
            solrServer.deleteByQuery(stringBuilder.toString());
            solrServer.commit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * pojo 方法解析
     *
     * @throws SolrServerException
     */
    @Test
    public void testQuery() throws SolrServerException, IOException {
        //创建连接
        CloudSolrServer solrServer = SolrServerSingle.getInstance(url);
        // 设置默认的collection
        solrServer.setDefaultCollection("collection2");
        Map<String,String> params = new HashMap<String,String>();
        params.put("q", "*:*");
        params.put("fq", "{!geofilt}");//距离过滤函数
        params.put("pt", "39.885609,116.338154");//当前经纬度
        params.put("sfield", "place_latlng");//经纬度的字段
        params.put("d", "15");//就近5公里的所有场馆
        params.put("sort", "geodist() asc");//根据距离排序
        params.put("fl", "*,place_dist:geodist(),score");
        params.put("start", "0");//记录开始位置
        params.put("rows", "10");//查询的行数
        params.put("df", "place_keywords");   // 设置默认搜素域

        QueryResponse response = solrServer.query(new MapSolrParams(params), SolrRequest.METHOD.POST);
        SolrDocumentList solrDocumentList = response.getResults();
        System.out.println("共查到记录：" + solrDocumentList.getNumFound());
        List<PlaceIndexInfo> placeIndexInfoList = response.getBeans(PlaceIndexInfo.class);
        System.out.println(placeIndexInfoList.size());
        System.out.println(JSON.json(placeIndexInfoList));
    }

    /**
     * 常规方法解析 方法解析
     *
     * @throws SolrServerException
     */
    @Test
    public void testQuery2() throws SolrServerException {

        SolrServer solrServer = new HttpSolrServer(url);
        SolrQuery solrQuery = new SolrQuery();
        StringBuilder stringBuilder=new StringBuilder();

        solrQuery.setQuery("place_name:");
        solrQuery.setQuery("place_cityId:");
        solrQuery.setQuery("place_districtId:");
        solrQuery.setQuery("place_categoryId:");
        solrQuery.setQuery("place_date:");
        solrQuery.setQuery("place_startTime:");
        solrQuery.setQuery("place_endTime:");
        solrQuery.setQuery("place_isSaled:");

        solrQuery.addSort("place_lowestPrice", SolrQuery.ORDER.asc);
        solrQuery.addSort("place_signupCount", SolrQuery.ORDER.desc);
        solrQuery.setStart(0);
        solrQuery.setRows(10);
        QueryResponse response = solrServer.query(solrQuery);
        SolrDocumentList solrDocumentList = response.getResults();
        System.out.println("共查到记录：" + solrDocumentList.getNumFound());

        List<PlaceIndexInfo> placeIndexInfoList = new ArrayList<PlaceIndexInfo>();
        for (SolrDocument solrDocument : solrDocumentList) {
            PlaceIndexInfo placeIndexInfo = new PlaceIndexInfo();
            //解析solr中数据
            String placeId = (String) solrDocument.get("place_id");
            String placeName = (String) solrDocument.get("place_name");
            List<Integer> placeCategoryIds = (List<Integer>) solrDocument.get("place_categoryId");
            List<String> placeCategoryName = (List<String>) solrDocument.get("place_categoryName");
            Integer placeCityId = (Integer) solrDocument.get("place_cityId");
            String placeCityName = (String) solrDocument.get("place_cityName");
            Integer placeDistrictId = (Integer) solrDocument.get("place_districtId");
            String placeDistrictName = (String) solrDocument.get("place_districtName");
            Double placeLat = (Double) solrDocument.get("place_lat");
            Double placeLng = (Double) solrDocument.get("place_lng");
            Integer placeCommentCount = (Integer) solrDocument.get("place_commentCount");
            Integer placeSignupCount = (Integer) solrDocument.get("place_signupCount");
            Double placeLowestPrice = (Double) solrDocument.get("place_lowestPrice");
            Double placePlaceScore = (Double) solrDocument.get("place_placeScore");
            String placeBizPlaceUrl = (String) solrDocument.get("place_bizPlaceUrl");
            List<Integer> placeSalePlanId = (List<Integer>) solrDocument.get("place_salePlanId");
            List<String> placeDate = (List<String>) solrDocument.get("place_date");
            List<String> placeStartTime = (List<String>) solrDocument.get("place_startTime");
            List<String> placeEndTime = (List<String>) solrDocument.get("place_endTime");
            List<Integer> placeIsSaled = (List<Integer>) solrDocument.get("place_isSaled");


        }


    }

    @Test
    public void query3(){
        ReqPlaceSearchInfo reqPlaceSearchInfo=new ReqPlaceSearchInfo();
        /**
         * 排序id 1、智能排序 2、价格排序 3、离我最近 4、人气最高
         * 如果没有传值默认为1
         */
        reqPlaceSearchInfo.setPlaceKeyName("aa");
        //reqPlaceSearchInfo.setCategoryId(1);
       /* reqPlaceSearchInfo.setDate("2015-08-15");
        List<String> startTimeList=new ArrayList<String>();
        startTimeList.add("15:00:00");
        startTimeList.add("18:00:00");
        startTimeList.add("20:00:00");
        reqPlaceSearchInfo.setStartTimeList(startTimeList);*/
        reqPlaceSearchInfo.setLat(39.843485);
        reqPlaceSearchInfo.setLng(116.471228);
        reqPlaceSearchInfo.setSortId(1);
        placeSearchService.searchPlaceInfoList(reqPlaceSearchInfo);
    }

    @Test
    public void query4(){
        ReqPlaceSearchInfo reqPlaceSearchInfo=new ReqPlaceSearchInfo();
        /**
         * 排序id 1、智能排序 2、价格排序 3、离我最近 4、人气最高
         * 如果没有传值默认为1
         */

        ReqClubMemberInfo reqClubMemberInfo=new ReqClubMemberInfo();
        //club_member_nickname:hello^1.9 AND club_id_mem:2
        reqClubMemberInfo.setClub_member_nickname("大熊");
        //reqClubMemberInfo.setClub_id(33);
        //reqClubMemberInfo.setClub_id(2);
        clubMemberSearchService.searchClubMemberInfoList(reqClubMemberInfo);
    }
    @Test
    public void query5(){
        ReqClubSearchInfo reqClubSearchInfo=new ReqClubSearchInfo();
        reqClubSearchInfo.setClub_name("牛逼");
        clubSearchService.searchClubInfoList(reqClubSearchInfo);


    }
    @Test
    public void query6(){
        ClubMemberIndexInfo clubMemberIndexInfo=new ClubMemberIndexInfo();
        clubMemberIndexInfo.setClub_role_name("tt");
        clubMemberIndexInfo.setClub_team_label_name("mm");
        clubMemberIndexInfo.setClub_member_nickname("啊里山的姑娘");
        clubMemberIndexInfo.setClub_id_mem(6+"");
        clubMemberIndexInfo.setClub_member_uid("123");

        clubMemberSearchService.insertDoc(clubMemberIndexInfo);


    }
    @Test
    public void delete() throws IOException, SolrServerException {
        //创建连接
        HttpSolrServer solrServer= new HttpSolrServer("http://101.52.128.118/solr/");
        // 删除所有的索引
        solrServer.deleteByQuery("SecuAbbr:阿里影业");
        solrServer.commit();
    }


    @Test
    public void query689() throws IOException, SolrServerException {
        //创建连接
        HttpSolrServer solrServer= new HttpSolrServer("http://101.52.128.118/solr/");
        //

        SolrQuery params = new SolrQuery();
        params.set("q", "kw:阿里");
        QueryResponse rsp = solrServer.query(params);
        SolrDocumentList docs = rsp.getResults();

        Iterator<SolrDocument> iter = docs.iterator();
        while (iter.hasNext()) {
            SolrDocument doc = iter.next();
            System.out.println(doc.getFieldValue("SecuAbbr"));
        }

    }
}
